package com.ybkj.daijia.server.event.listener.zhuan;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.ZhuanOrderTimerTask;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderPaidanEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Order;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ZhuanOrderPaidanEventListener implements ApplicationListener<ZhuanOrderPaidanEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhuanOrderPaidanEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ZhuancheService zhuancheService;

    @Override
    @Async
    public void onApplicationEvent(ZhuanOrderPaidanEvent event) {
        Long orderId = (Long) event.getSource();

        Order order = zhuancheService.findOne(orderId);

        if (order.getStatus().equals(Order.ORDER_STATUS_PAIDAN)) {
            Driver driver = driverService.findOne(order.getEmployId());

            try {
                //发送设备推送
                ZhuanOrderTimerTask.pushMsToDriverPhoneByJPush(order, driver, settingUtils, ",赶快打开服务人员端接单吧");
            } catch (IOException e) {
                logger.error("push error : ", e);
            }

            //发送短信
            Company driverCompany = companyService.findOne(driver.getCompanyId());
            Company finalCompany = driverCompany;
            Company main = companyService.getMain();
            Setting setting = settingUtils.get();
            if (!main.getId().equals(driverCompany.getId()) && setting.isSmsMainCompany()) {
                finalCompany = main;
            }
            try {
                SmsTemplet templet = smsService.findByAlias(finalCompany.getId(), SmsType.newOrder);
                if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
                    DateFormat format = new SimpleDateFormat("HH:mm");
                    String serverTime = format.format(order.getServerTime());
                    //smsService.sendSms(driver.getPhone(),"paiZhuancheOrder", "{\"订单号\":\""+"***"+order.getOrderNo().substring(8)+"\",\"客户姓名\":\""+order.getPassengerName()+"\",\"客户电话\":\""+order.getPassengerPhone()+"\",\"预约时间\":\""+serverTime+"\",\"预约地点\":\""+order.getStartAddress()+"\"}",finalCompany.getId(), 0, finalCompany.getSignName());
                    //短信发送（改）
                    JSONObject smsParam = new JSONObject();
                    smsParam.put("order", order.getOrderNo().substring(8));
                    smsParam.put("name", order.getPassengerName());
                    smsParam.put("tel", order.getPassengerPhone());
                    smsParam.put("serverTime", serverTime);
                    smsParam.put("fromPlace", order.getStartAddress());
                    smsService.sendSms(driver.getPhone(), templet.getTemplateId(),
                        smsParam.toJSONString(), finalCompany.getId(), 0,
                        finalCompany.getSignName());
                }
            } catch (Exception e) {
                logger.error("派单短信  发送短信发生异常：{}", e.getMessage());
            }
        }


    }

}
